import { KeyCodeInfo, KeyInfo } from "../types/types";
import { MouseKey } from "../types/types.v1";
import newKeyInfos from "@/src/config/newKeyInfos.json";

export const basicKeyToByte = {
  KC_NO: 0x0000,
  KC_TRNS: 0x0001,
  KC_A: 0x0004,
  KC_B: 0x0005,
  KC_C: 0x0006,
  KC_D: 0x0007,
  KC_E: 0x0008,
  KC_F: 0x0009,
  KC_G: 0x000a,
  KC_H: 0x000b,
  KC_I: 0x000c,
  KC_J: 0x000d,
  KC_K: 0x000e,
  KC_L: 0x000f,
  KC_M: 0x0010,
  KC_N: 0x0011,
  KC_O: 0x0012,
  KC_P: 0x0013,
  KC_Q: 0x0014,
  KC_R: 0x0015,
  KC_S: 0x0016,
  KC_T: 0x0017,
  KC_U: 0x0018,
  KC_V: 0x0019,
  KC_W: 0x001a,
  KC_X: 0x001b,
  KC_Y: 0x001c,
  KC_Z: 0x001d,
  KC_1: 0x001e,
  KC_2: 0x001f,
  KC_3: 0x0020,
  KC_4: 0x0021,
  KC_5: 0x0022,
  KC_6: 0x0023,
  KC_7: 0x0024,
  KC_8: 0x0025,
  KC_9: 0x0026,
  KC_0: 0x0027,
  KC_ENT: 0x0028,
  KC_ESC: 0x0029,
  KC_BSPC: 0x002a,
  KC_TAB: 0x002b,
  KC_SPC: 0x002c,
  KC_MINS: 0x002d,
  KC_EQL: 0x002e,
  KC_LBRC: 0x002f,
  KC_RBRC: 0x0030,
  KC_BSLS: 0x0031,
  KC_NUHS: 0x0032,
  KC_SCLN: 0x0033,
  KC_QUOT: 0x0034,
  KC_GRV: 0x0035,
  KC_COMM: 0x0036,
  KC_DOT: 0x0037,
  KC_SLSH: 0x0038,
  KC_CAPS: 0x0039,
  KC_F1: 0x003a,
  KC_F2: 0x003b,
  KC_F3: 0x003c,
  KC_F4: 0x003d,
  KC_F5: 0x003e,
  KC_F6: 0x003f,
  KC_F7: 0x0040,
  KC_F8: 0x0041,
  KC_F9: 0x0042,
  KC_F10: 0x0043,
  KC_F11: 0x0044,
  KC_F12: 0x0045,
  KC_PSCR: 0x0046,
  KC_SLCK: 0x0047,
  KC_PAUS: 0x0048,
  KC_INS: 0x0049,
  KC_HOME: 0x004a,
  KC_PGUP: 0x004b,
  KC_DEL: 0x004c,
  KC_END: 0x004d,
  KC_PGDN: 0x004e,
  KC_RGHT: 0x004f,
  KC_LEFT: 0x0050,
  KC_DOWN: 0x0051,
  KC_UP: 0x0052,
  KC_NLCK: 0x0053,
  KC_PSLS: 0x0054,
  KC_PAST: 0x0055,
  KC_PMNS: 0x0056,
  KC_PPLS: 0x0057,
  KC_PENT: 0x0058,
  KC_P1: 0x0059,
  KC_P2: 0x005a,
  KC_P3: 0x005b,
  KC_P4: 0x005c,
  KC_P5: 0x005d,
  KC_P6: 0x005e,
  KC_P7: 0x005f,
  KC_P8: 0x0060,
  KC_P9: 0x0061,
  KC_P0: 0x0062,
  KC_PDOT: 0x0063,
  KC_NUBS: 0x0064,
  KC_APP: 0x0065,
  KC_POWER: 0x0066,
  KC_PEQL: 0x0067,
  KC_F13: 0x0068,
  KC_F14: 0x0069,
  KC_F15: 0x006a,
  KC_F16: 0x006b,
  KC_F17: 0x006c,
  KC_F18: 0x006d,
  KC_F19: 0x006e,
  KC_F20: 0x006f,
  KC_F21: 0x0070,
  KC_F22: 0x0071,
  KC_F23: 0x0072,
  KC_F24: 0x0073,
  KC_EXECUTE: 0x0074,
  KC_HELP: 0x0075,
  KC_MENU: 0x0076,
  KC_SELECT: 0x0077,
  KC_STOP: 0x0078,
  KC_AGAIN: 0x0079,
  KC_UNDO: 0x007a,
  KC_CUT: 0x007b,
  KC_COPY: 0x007c,
  KC_PASTE: 0x007d,
  KC_FIND: 0x007e,
  KC_LCAP: 0x0082,
  KC_LNUM: 0x0083,
  KC_LSCR: 0x0084,
  KC_PCMM: 0x0085,
  KC_KP_EQUAL_AS400: 0x0086,
  KC_RO: 0x0087,
  KC_KANA: 0x0088,
  KC_JYEN: 0x0089,
  KC_HENK: 0x008a,
  KC_MHEN: 0x008b,
  KC_INT6: 0x008c,
  KC_INT7: 0x008d,
  KC_INT8: 0x008e,
  KC_INT9: 0x008f,
  KC_HAEN: 0x0090,
  KC_HANJ: 0x0091,
  KC_LANG3: 0x0092,
  KC_LANG4: 0x0093,
  KC_LANG5: 0x0094,
  KC_LANG6: 0x0095,
  KC_LANG7: 0x0096,
  KC_LANG8: 0x0097,
  KC_LANG9: 0x0098,
  KC_ERAS: 0x0099,
  KC_SYSREQ: 0x009a,
  KC_CANCEL: 0x009b,
  KC_CLR: 0x009c,
  KC_CLEAR: 0x009c,
  KC_PRIOR: 0x009d,
  KC_OUT: 0x00a0,
  KC_OPER: 0x00a1,
  KC_CLEAR_AGAIN: 0x00a2,
  KC_CRSEL: 0x00a3,
  KC_EXSEL: 0x00a4,
  KC_PWR: 0x00a5,
  KC_SLEP: 0x00a6,
  KC_WAKE: 0x00a7,
  KC_MUTE: 0x00a8,
  KC_VOLU: 0x00a9,
  KC_VOLD: 0x00aa,
  KC_MNXT: 0x00ab,
  KC_MPRV: 0x00ac,
  KC_MSTP: 0x00ad,
  KC_MPLY: 0x00ae,
  KC_MSEL: 0x00af,
  KC_EJCT: 0x00b0,
  KC_MAIL: 0x00b1,
  KC_CALC: 0x00b2,
  KC_MYCM: 0x00b3,
  KC_WWW_SEARCH: 0x00b4,
  KC_WWW_HOME: 0x00b5,
  KC_WWW_BACK: 0x00b6,
  KC_WWW_FORWARD: 0x00b7,
  KC_WWW_STOP: 0x00b8,
  KC_WWW_REFRESH: 0x00b9,
  KC_WWW_FAVORITES: 0x00ba,
  KC_MFFD: 0x00bb,
  KC_MRWD: 0x00bc,
  KC_BRIU: 0x00bd,
  KC_BRID: 0x00be,
  KC_LCTL: 0x00e0,
  KC_LSFT: 0x00e1,
  KC_LALT: 0x00e2,
  KC_LGUI: 0x00e3,
  KC_RCTL: 0x00e4,
  KC_RSFT: 0x00e5,
  KC_RALT: 0x00e6,
  KC_RGUI: 0x00e7,
  KC_MS_UP: 0x00f0,
  KC_MS_DOWN: 0x00f1,
  KC_MS_LEFT: 0x00f2,
  KC_MS_RIGHT: 0x00f3,
  KC_MS_BTN1: 0x00f4,
  KC_MS_BTN2: 0x00f5,
  KC_MS_BTN3: 0x00f6,
  KC_MS_BTN4: 0x00f7,
  KC_MS_BTN5: 0x00f8,
  KC_MS_WH_UP: 0x00f9,
  KC_MS_WH_DOWN: 0x00fa,
  KC_MS_WH_LEFT: 0x00fb,
  KC_MS_WH_RIGHT: 0x00fc,
  KC_MS_ACCEL0: 0x00fd,
  KC_MS_ACCEL1: 0x00fe,
  KC_MS_ACCEL2: 0x00ff,
  RESET: 0x5c00,
  DEBUG: 0x5c01,
  MAGIC_TOGGLE_NKRO: 0x5c14,
  KC_GESC: 0x5c16,
  AU_ON: 0x5c1d,
  AU_OFF: 0x5c1e,
  AU_TOG: 0x5c1f,
  CLICKY_TOGGLE: 0x5c20,
  CLICKY_ENABLE: 0x5c21,
  CLICKY_DISABLE: 0x5c22,
  CLICKY_UP: 0x5c23,
  CLICKY_DOWN: 0x5c24,
  CLICKY_RESET: 0x5c25,
  MU_ON: 0x5c26,
  MU_OFF: 0x5c27,
  MU_TOG: 0x5c28,
  MU_MOD: 0x5c29,
  BL_ON: 0x5cbb,
  BL_OFF: 0x5cbc,
  BL_DEC: 0x5cbd,
  BL_INC: 0x5cbe,
  BL_TOGG: 0x5cbf,
  BL_STEP: 0x5cc0,
  BL_BRTG: 0x5cc1,
  RGB_TOG: 0x5cc2,
  RGB_MOD: 0x5cc3,
  RGB_RMOD: 0x5cc4,
  RGB_HUI: 0x5cc5,
  RGB_HUD: 0x5cc6,
  RGB_SAI: 0x5cc7,
  RGB_SAD: 0x5cc8,
  RGB_VAI: 0x5cc9,
  RGB_VAD: 0x5cca,
  RGB_SPI: 0x5ccb,
  RGB_SPD: 0x5ccc,
  RGB_M_P: 0x5ccd,
  RGB_M_B: 0x5cce,
  RGB_M_R: 0x5ccf,
  RGB_M_SW: 0x5cd0,
  RGB_M_SN: 0x5cd1,
  RGB_M_K: 0x5cd2,
  RGB_M_X: 0x5cd3,
  RGB_M_G: 0x5cd4,
  KC_LSPO: 0x5cd7,
  KC_RSPC: 0x5cd8,
  KC_SFTENT: 0x5cd9,
  KC_LCPO: 0x5cf3,
  KC_RCPC: 0x5cf4,
  KC_LAPO: 0x5cf5,
  KC_RAPC: 0x5cf6,
  BR_INC: 0x5f00,
  BR_DEC: 0x5f01,
  EF_INC: 0x5f02,
  EF_DEC: 0x5f03,
  ES_INC: 0x5f04,
  ES_DEC: 0x5f05,
  H1_INC: 0x5f06,
  H1_DEC: 0x5f07,
  S1_INC: 0x5f08,
  S1_DEC: 0x5f09,
  H2_INC: 0x5f0a,
  H2_DEC: 0x5f0b,
  S2_INC: 0x5f0c,
  S2_DEC: 0x5f0d,
  FN_MO13: 0x5f10,
  FN_MO23: 0x5f11,
};

const evtToKeyByte = {
  Digit1: basicKeyToByte.KC_1,
  Digit2: basicKeyToByte.KC_2,
  Digit3: basicKeyToByte.KC_3,
  Digit4: basicKeyToByte.KC_4,
  Digit5: basicKeyToByte.KC_5,
  Digit6: basicKeyToByte.KC_6,
  Digit7: basicKeyToByte.KC_7,
  Digit8: basicKeyToByte.KC_8,
  Digit9: basicKeyToByte.KC_9,
  Digit0: basicKeyToByte.KC_0,
  KeyA: basicKeyToByte.KC_A,
  KeyB: basicKeyToByte.KC_B,
  KeyC: basicKeyToByte.KC_C,
  KeyD: basicKeyToByte.KC_D,
  KeyE: basicKeyToByte.KC_E,
  KeyF: basicKeyToByte.KC_F,
  KeyG: basicKeyToByte.KC_G,
  KeyH: basicKeyToByte.KC_H,
  KeyI: basicKeyToByte.KC_I,
  KeyJ: basicKeyToByte.KC_J,
  KeyK: basicKeyToByte.KC_K,
  KeyL: basicKeyToByte.KC_L,
  KeyM: basicKeyToByte.KC_M,
  KeyN: basicKeyToByte.KC_N,
  KeyO: basicKeyToByte.KC_O,
  KeyP: basicKeyToByte.KC_P,
  KeyQ: basicKeyToByte.KC_Q,
  KeyR: basicKeyToByte.KC_R,
  KeyS: basicKeyToByte.KC_S,
  KeyT: basicKeyToByte.KC_T,
  KeyU: basicKeyToByte.KC_U,
  KeyV: basicKeyToByte.KC_V,
  KeyW: basicKeyToByte.KC_W,
  KeyX: basicKeyToByte.KC_X,
  KeyY: basicKeyToByte.KC_Y,
  KeyZ: basicKeyToByte.KC_Z,
  Comma: basicKeyToByte.KC_COMM,
  Period: basicKeyToByte.KC_DOT,
  Semicolon: basicKeyToByte.KC_SCLN,
  Quote: basicKeyToByte.KC_QUOT,
  BracketLeft: basicKeyToByte.KC_LBRC,
  BracketRight: basicKeyToByte.KC_RBRC,
  Backspace: basicKeyToByte.KC_BSPC,
  Backquote: basicKeyToByte.KC_GRV,
  Slash: basicKeyToByte.KC_SLSH,
  Backslash: basicKeyToByte.KC_BSLS,
  Minus: basicKeyToByte.KC_MINS,
  Equal: basicKeyToByte.KC_EQL,
  IntlRo: basicKeyToByte.KC_RO,
  IntlYen: basicKeyToByte.KC_JYEN,
  AltLeft: basicKeyToByte.KC_LALT,
  AltRight: basicKeyToByte.KC_RALT,
  CapsLock: basicKeyToByte.KC_CAPS,
  ControlLeft: basicKeyToByte.KC_LCTL,
  ControlRight: basicKeyToByte.KC_RCTL,
  MetaLeft: basicKeyToByte.KC_LGUI,
  MetaRight: basicKeyToByte.KC_RGUI,
  OSLeft: basicKeyToByte.KC_LGUI,
  OSRight: basicKeyToByte.KC_RGUI,
  ShiftLeft: basicKeyToByte.KC_LSFT,
  ShiftRight: basicKeyToByte.KC_RSFT,
  ContextMenu: basicKeyToByte.KC_APP,
  Enter: basicKeyToByte.KC_ENT,
  Space: basicKeyToByte.KC_SPC,
  Tab: basicKeyToByte.KC_TAB,
  Delete: basicKeyToByte.KC_DEL,
  End: basicKeyToByte.KC_END,
  Help: basicKeyToByte.KC_HELP,
  Home: basicKeyToByte.KC_HOME,
  Insert: basicKeyToByte.KC_INS,
  PageDown: basicKeyToByte.KC_PGDN,
  PageUp: basicKeyToByte.KC_PGUP,
  ArrowDown: basicKeyToByte.KC_DOWN,
  ArrowLeft: basicKeyToByte.KC_LEFT,
  ArrowRight: basicKeyToByte.KC_RGHT,
  ArrowUp: basicKeyToByte.KC_UP,
  Escape: basicKeyToByte.KC_ESC,
  PrintScreen: basicKeyToByte.KC_PSCR,
  ScrollLock: basicKeyToByte.KC_SLCK,
  AudioVolumeUp: basicKeyToByte.KC_VOLU,
  AudioVolumeDown: basicKeyToByte.KC_VOLD,
  AudioVolumeMute: basicKeyToByte.KC_MUTE,
  Pause: basicKeyToByte.KC_PAUS,
  F1: basicKeyToByte.KC_F1,
  F2: basicKeyToByte.KC_F2,
  F3: basicKeyToByte.KC_F3,
  F4: basicKeyToByte.KC_F4,
  F5: basicKeyToByte.KC_F5,
  F6: basicKeyToByte.KC_F6,
  F7: basicKeyToByte.KC_F7,
  F8: basicKeyToByte.KC_F8,
  F9: basicKeyToByte.KC_F9,
  F10: basicKeyToByte.KC_F10,
  F11: basicKeyToByte.KC_F11,
  F12: basicKeyToByte.KC_F12,
  F13: basicKeyToByte.KC_F13,
  F14: basicKeyToByte.KC_F14,
  F15: basicKeyToByte.KC_F15,
  F16: basicKeyToByte.KC_F16,
  F17: basicKeyToByte.KC_F17,
  F18: basicKeyToByte.KC_F18,
  F19: basicKeyToByte.KC_F19,
  F20: basicKeyToByte.KC_F20,
  F21: basicKeyToByte.KC_F21,
  F22: basicKeyToByte.KC_F22,
  F23: basicKeyToByte.KC_F23,
  F24: basicKeyToByte.KC_F24,
  NumLock: basicKeyToByte.KC_NLCK,
  Numpad0: basicKeyToByte.KC_P0,
  Numpad1: basicKeyToByte.KC_P1,
  Numpad2: basicKeyToByte.KC_P2,
  Numpad3: basicKeyToByte.KC_P3,
  Numpad4: basicKeyToByte.KC_P4,
  Numpad5: basicKeyToByte.KC_P5,
  Numpad6: basicKeyToByte.KC_P6,
  Numpad7: basicKeyToByte.KC_P7,
  Numpad8: basicKeyToByte.KC_P8,
  Numpad9: basicKeyToByte.KC_P9,
  NumpadAdd: basicKeyToByte.KC_PPLS,
  NumpadComma: basicKeyToByte.KC_COMM,
  NumpadDecimal: basicKeyToByte.KC_PDOT,
  NumpadDivide: basicKeyToByte.KC_PSLS,
  NumpadEnter: basicKeyToByte.KC_PENT,
  NumpadEqual: basicKeyToByte.KC_PEQL,
  NumpadMultiply: basicKeyToByte.KC_PAST,
  NumpadSubtract: basicKeyToByte.KC_PMNS,
};
const keyName = {
  KC_NO: "",
  KC_TRNS: "▽",
  KC_A: "A",
  KC_B: "B",
  KC_C: "C",
  KC_D: "D",
  KC_E: "E",
  KC_F: "F",
  KC_G: "G",
  KC_H: "H",
  KC_I: "I",
  KC_J: "J",
  KC_K: "K",
  KC_L: "L",
  KC_M: "M",
  KC_N: "N",
  KC_O: "O",
  KC_P: "P",
  KC_Q: "Q",
  KC_R: "R",
  KC_S: "S",
  KC_T: "T",
  KC_U: "U",
  KC_V: "V",
  KC_W: "W",
  KC_X: "X",
  KC_Y: "Y",
  KC_Z: "Z",
  KC_1: "1 !",
  KC_2: "2 @",
  KC_3: "3 #",
  KC_4: "4 $",
  KC_5: "5 %",
  KC_6: "6 ^",
  KC_7: "7 &",
  KC_8: "8 *",
  KC_9: "9 (",
  KC_0: "0 )",
  KC_ENT: "ENTER",
  KC_ESC: "ESC",
  KC_BSPC: "BACKSPACE",
  KC_TAB: "TAB",
  KC_SPC: "SPACE",
  KC_MINS: "- _",
  KC_EQL: "= +",
  KC_LBRC: "[ {",
  KC_RBRC: "] }",
  KC_BSLS: "\\ |",
  KC_SCLN: "; :",
  KC_QUOT: "' \"",
  KC_GRV: "`",
  KC_COMM: ", <",
  KC_DOT: ". >",
  KC_SLSH: "/ ?",
  KC_CAPS: "CAPS",
  KC_F1: "F1",
  KC_F2: "F2",
  KC_F3: "F3",
  KC_F4: "F4",
  KC_F5: "F5",
  KC_F6: "F6",
  KC_F7: "F7",
  KC_F8: "F8",
  KC_F9: "F9",
  KC_F10: "F10",
  KC_F11: "F11",
  KC_F12: "F12",
  KC_F13: "F13",
  KC_PSCR: "PR",
  KC_SLCK: "SC",
  KC_PAUS: "PS",
  KC_INS: "INS",
  KC_HOME: "HOME",
  KC_PGUP: "PGUP",
  KC_DEL: "DEL",
  KC_END: "END",
  KC_PGDN: "PGDN",
  KC_RGHT: "→",
  KC_LEFT: "←",
  KC_DOWN: "↓",
  KC_UP: "↑",
  KC_NLCK: "N.Lck",
  KC_PSLS: "÷",
  KC_PAST: "×",
  KC_PMNS: "-",
  KC_PPLS: "+",
  KC_PENT: "N.Ent",
  KC_PDOT: ".",
  KC_PEQL: "=",
  KC_APP: "MENU",
  KC_HELP: "Help",
  KC_RO: "/",
  KC_LALT: "ALT",
  KC_RALT: "ALT",
  KC_LCTL: "CTRL",
  KC_RCTL: "CTRL",
  KC_LGUI: "WIN",
  KC_RGUI: "WIN",
  KC_LSFT: "SHIFT",
  KC_RSFT: "SHIFT",
  KC_VOLU: "Vol+",
  KC_VOLD: "Vol-",
  KC_MUTE: "Mute",
  KC_P1: "Num 1",
  KC_P2: "Num 2",
  KC_P3: "Num 3",
  KC_P4: "Num 4",
  KC_P5: "Num 5",
  KC_P6: "Num 6",
  KC_P7: "Num 7",
  KC_P8: "Num 8",
  KC_P9: "Num 9",
  KC_P0: "Num 0",
  KC_MAC_WIN: "多窗口",
  KC_MAC_TASK: "多任务",
};

const evtKeyToName = {
  Digit1: keyName.KC_1,
  Digit2: keyName.KC_2,
  Digit3: keyName.KC_3,
  Digit4: keyName.KC_4,
  Digit5: keyName.KC_5,
  Digit6: keyName.KC_6,
  Digit7: keyName.KC_7,
  Digit8: keyName.KC_8,
  Digit9: keyName.KC_9,
  Digit0: keyName.KC_0,
  KeyA: keyName.KC_A,
  KeyB: keyName.KC_B,
  KeyC: keyName.KC_C,
  KeyD: keyName.KC_D,
  KeyE: keyName.KC_E,
  KeyF: keyName.KC_F,
  KeyG: keyName.KC_G,
  KeyH: keyName.KC_H,
  KeyI: keyName.KC_I,
  KeyJ: keyName.KC_J,
  KeyK: keyName.KC_K,
  KeyL: keyName.KC_L,
  KeyM: keyName.KC_M,
  KeyN: keyName.KC_N,
  KeyO: keyName.KC_O,
  KeyP: keyName.KC_P,
  KeyQ: keyName.KC_Q,
  KeyR: keyName.KC_R,
  KeyS: keyName.KC_S,
  KeyT: keyName.KC_T,
  KeyU: keyName.KC_U,
  KeyV: keyName.KC_V,
  KeyW: keyName.KC_W,
  KeyX: keyName.KC_X,
  KeyY: keyName.KC_Y,
  KeyZ: keyName.KC_Z,
  Comma: keyName.KC_COMM,
  Period: keyName.KC_DOT,
  Semicolon: keyName.KC_SCLN,
  Quote: keyName.KC_QUOT,
  BracketLeft: keyName.KC_LBRC,
  BracketRight: keyName.KC_RBRC,
  Backspace: keyName.KC_BSPC,
  Backquote: keyName.KC_GRV,
  Slash: keyName.KC_SLSH,
  Backslash: keyName.KC_BSLS,
  Minus: keyName.KC_MINS,
  Equal: keyName.KC_EQL,
  IntlRo: keyName.KC_RO,
  AltLeft: keyName.KC_LALT,
  AltRight: keyName.KC_RALT,
  CapsLock: keyName.KC_CAPS,
  ControlLeft: keyName.KC_LCTL,
  ControlRight: keyName.KC_RCTL,
  MetaLeft: keyName.KC_LGUI,
  MetaRight: keyName.KC_RGUI,
  OSLeft: keyName.KC_LGUI,
  OSRight: keyName.KC_RGUI,
  ShiftLeft: keyName.KC_LSFT,
  ShiftRight: keyName.KC_RSFT,
  ContextMenu: keyName.KC_APP,
  Enter: keyName.KC_ENT,
  Space: keyName.KC_SPC,
  Tab: keyName.KC_TAB,
  Delete: keyName.KC_DEL,
  End: keyName.KC_END,
  Help: keyName.KC_HELP,
  Home: keyName.KC_HOME,
  Insert: keyName.KC_INS,
  PageDown: keyName.KC_PGDN,
  PageUp: keyName.KC_PGUP,
  ArrowDown: keyName.KC_DOWN,
  ArrowLeft: keyName.KC_LEFT,
  ArrowRight: keyName.KC_RGHT,
  ArrowUp: keyName.KC_UP,
  Escape: keyName.KC_ESC,
  PrintScreen: keyName.KC_PSCR,
  ScrollLock: keyName.KC_SLCK,
  AudioVolumeUp: keyName.KC_VOLU,
  AudioVolumeDown: keyName.KC_VOLD,
  AudioVolumeMute: keyName.KC_MUTE,
  Pause: keyName.KC_PAUS,
  F1: keyName.KC_F1,
  F2: keyName.KC_F2,
  F3: keyName.KC_F3,
  F4: keyName.KC_F4,
  F5: keyName.KC_F5,
  F6: keyName.KC_F6,
  F7: keyName.KC_F7,
  F8: keyName.KC_F8,
  F9: keyName.KC_F9,
  F10: keyName.KC_F10,
  F11: keyName.KC_F11,
  F12: keyName.KC_F12,
  F13: keyName.KC_F13,
  NumLock: keyName.KC_NLCK,
  Numpad0: keyName.KC_P0,
  Numpad1: keyName.KC_P1,
  Numpad2: keyName.KC_P2,
  Numpad3: keyName.KC_P3,
  Numpad4: keyName.KC_P4,
  Numpad5: keyName.KC_P5,
  Numpad6: keyName.KC_P6,
  Numpad7: keyName.KC_P7,
  Numpad8: keyName.KC_P8,
  Numpad9: keyName.KC_P9,
  NumpadAdd: keyName.KC_PPLS,
  NumpadComma: keyName.KC_COMM,
  NumpadDecimal: keyName.KC_PDOT,
  NumpadDivide: keyName.KC_PSLS,
  NumpadEnter: keyName.KC_PENT,
  NumpadEqual: keyName.KC_PEQL,
  NumpadMultiply: keyName.KC_PAST,
  NumpadSubtract: keyName.KC_PMNS,
};

const keyCodes = [
  { code: 0x0004, key: "A" },
  { code: 0x0005, key: "B" },
  { code: 0x0006, key: "C" },
  { code: 0x0007, key: "D" },
  { code: 0x0008, key: "E" },
  { code: 0x0009, key: "F" },
  { code: 0x000a, key: "G" },
  { code: 0x000b, key: "H" },
  { code: 0x000c, key: "I" },
  { code: 0x000d, key: "J" },
  { code: 0x000e, key: "K" },
  { code: 0x000f, key: "L" },
  { code: 0x0010, key: "M" },
  { code: 0x0011, key: "N" },
  { code: 0x0012, key: "O" },
  { code: 0x0013, key: "P" },
  { code: 0x0014, key: "Q" },
  { code: 0x0015, key: "R" },
  { code: 0x0016, key: "S" },
  { code: 0x0017, key: "T" },
  { code: 0x0018, key: "U" },
  { code: 0x0019, key: "V" },
  { code: 0x001a, key: "W" },
  { code: 0x001b, key: "X" },
  { code: 0x001c, key: "Y" },
  { code: 0x001d, key: "Z" },
  { code: 0x001e, key: "1" },
  { code: 0x001f, key: "2" },
  { code: 0x0020, key: "3" },
  { code: 0x0021, key: "4" },
  { code: 0x0022, key: "5" },
  { code: 0x0023, key: "6" },
  { code: 0x0024, key: "7" },
  { code: 0x0025, key: "8" },
  { code: 0x0026, key: "9" },
  { code: 0x0027, key: "0" },
  { code: 0x0028, key: "Return" },
  { code: 0x0029, key: "Esc" },
  { code: 0x002a, key: "Backspace" },
  { code: 0x002b, key: "Tab" },
  { code: 0x002c, key: "Space" },
  { code: 0x002d, key: "-\n_" },
  { code: 0x002e, key: "=\n+" },
  { code: 0x002f, key: "[\n{" },
  { code: 0x0030, key: "]\n}" },
  { code: 0x0031, key: "\\\n|" },
  { code: 0x0032, key: " " },
  { code: 0x0033, key: ";\n:" },
  { code: 0x0034, key: "'\n\"" },
  { code: 0x0035, key: "`\n~" },
  { code: 0x0036, key: ",\n<" },
  { code: 0x0037, key: ".\n>" },
  { code: 0x0038, key: "/\n?" },
  { code: 0x0039, key: "Caps Lock" },
  { code: 0x003a, key: "F1" },
  { code: 0x003b, key: "F2" },
  { code: 0x003c, key: "F3" },
  { code: 0x003d, key: "F4" },
  { code: 0x003e, key: "F5" },
  { code: 0x003f, key: "F6" },
  { code: 0x0040, key: "F7" },
  { code: 0x0041, key: "F8" },
  { code: 0x0042, key: "F9" },
  { code: 0x0043, key: "F10" },
  { code: 0x0044, key: "F11" },
  { code: 0x0045, key: "F12" },
  { code: 0x0053, key: "Num Lock" },
  { code: 0x0054, key: "Num /" },
  { code: 0x0055, key: "Num *" },
  { code: 0x0056, key: "Num -" },
  { code: 0x0057, key: "Num +" },
  { code: 0x0059, key: "Num 1" },
  { code: 0x005a, key: "Num 2" },
  { code: 0x005b, key: "Num 3" },
  { code: 0x005c, key: "Num 4" },
  { code: 0x005d, key: "Num 5" },
  { code: 0x005e, key: "Num 6" },
  { code: 0x005f, key: "Num 7" },
  { code: 0x0060, key: "Num 8" },
  { code: 0x0061, key: "Num 9" },
  { code: 0x0062, key: "Num 0" },
  { code: 0x0063, key: "Num ." },
  { code: 0x0058, key: "Num Enter" },
];

export const eventToKeyName = (key: string) => {
  return evtKeyToName[key as keyof typeof evtKeyToName] || "";
};

export const mergeKey = (mouseKey: MouseKey, keyInfo: KeyInfo, t: Function) => {
  return {
    ...mouseKey,
    type: keyInfo.new_type,
    code1: keyInfo.new_code1,
    code2: keyInfo.new_code2,
    code3: keyInfo.new_code3,
    name: t(keyInfo.lang) as string,
  };
};

export const mergeMouseKey = (key1: MouseKey, key2: MouseKey, t: Function) => {
  return {
    ...key1,
    type: key2.type,
    code1: key2.code1,
    code2: key2.code2,
    code3: key2.code3,
    lang: key2.lang,
    name: key2.lang ? (t(key2.lang) as string) : key2.name,
  };
};

export const isSameMouseKey = (key1: MouseKey, key2: MouseKey) => {
  return (
    key1.type == key2.type &&
    key1.code1 == key2.code1 &&
    key1.code2 == key2.code2 &&
    key1.code3 == key2.code3
  );
};

export function eventCode2Keycode(code: string) {
  return evtToKeyByte[code as keyof typeof evtToKeyByte];
}

export const getKeyInfoName = (
  keycodeInfos: KeyCodeInfo[],
  new_type: number,
  new_code1: number,
  new_code2: number,
  new_code3: number,
  t: Function
) => {
  // 1. 检查系统按键
  const systemKey = newKeyInfos.systemKey?.list
    .flatMap((group) => group.keycodes)
    .find(
      (key) =>
        Number(key.type) === new_type &&
        key.code1 === new_code1 &&
        key.code2 === new_code2 &&
        key.code3 === new_code3
    );
  if (systemKey) return t(systemKey.name);

  // 2. 检查其他按键
  const otherKey = newKeyInfos.Other?.list
    .flatMap((group) => group.keycodes)
    .find(
      (key) =>
        Number(key.type) === new_type &&
        key.code1 === new_code1 &&
        key.code2 === new_code2 &&
        key.code3 === new_code3
    );
  if (otherKey) return t(otherKey.name);

  // 3. 检查键盘按键
  const keyboardKey = newKeyInfos.Keyboard?.list
    .flatMap((group) => group.keycodes)
    .find(
      (key) =>
        Number(key.type) === new_type &&
        key.code1 === new_code1 &&
        key.code2 === new_code2 &&
        key.code3 === new_code3
    );
  if (keyboardKey) return keyboardKey.name;

  // 4. 处理组合键
  if (new_type === 0x10) {
    const key = keycodeInfos.find(
      (key) => key.type === 0x20 && key.code2 === new_code2
    );
    const modifiers = getModifiers(new_code1);
    return [...modifiers, key?.name || ""].filter(Boolean).join("+");
  }

  // 5. 处理宏按键
  if (new_type === 0x70) {
    return `${t("530")}${new_code1 + 1}`;
  }

  // 6. 查找普通按键
  const key = keycodeInfos.find(
    (key) =>
      key.type === new_type &&
      key.code1 === new_code1 &&
      key.code2 === new_code2 &&
      key.code3 === new_code3
  );

  if (
    new_type === 0x50 &&
    new_code1 >= 0 &&
    new_code2 === 0 &&
    new_code3 === 0
  ) {
    return t("openApp");
  }

  return key ? (key.lang ? t(key.lang) : key.name) : "";
};

// 封装获取修饰键的逻辑
const getModifiers = (new_code1: number) => {
  const alt = (new_code1 >> 2) & 0x1 || (new_code1 >> 6) & 0x1 ? "ALT" : "";
  const shift = (new_code1 >> 1) & 0x1 || (new_code1 >> 5) & 0x1 ? "SHIFT" : "";
  const ctrl = new_code1 & 0x1 || (new_code1 >> 4) & 0x1 ? "CTRL" : "";
  const win = (new_code1 >> 3) & 0x1 || (new_code1 >> 7) & 0x1 ? "WIN" : "";

  return [ctrl, win, alt, shift].filter((e) => e !== ""); // 过滤空值
};

export const getMouseKeyName = (mouseKey: MouseKey) => {
  if (mouseKey.type == 0x10) {
    return getKeycodeCtrlName(mouseKey.code1, mouseKey.code2);
  }
  return "";
};

export const getKeycodeCtrlName = (code1: number, code2: number) => {
  let ctrlName = [];
  if (code1 & 0x1 || (code1 >> 4) & 0x1) {
    ctrlName.push("CTRL");
  }
  if ((code1 >> 1) & 0x1 || (code1 >> 5) & 0x1) {
    ctrlName.push("SHIFT");
  }
  if ((code1 >> 2) & 0x1 || (code1 >> 6) & 0x1) {
    ctrlName.push("ALT");
  }
  if ((code1 >> 3) & 0x1 || (code1 >> 7) & 0x1) {
    ctrlName.push("WIN");
  }

  const keyCode = keyCodes.find((keycode) => keycode.code == code2);
  if (keyCode) {
    ctrlName.push(keyCode.key);
  }

  return ctrlName.join("+");
};
